package hankz.spring.beans.factory.support;

import hankz.spring.beans.factory.config.BeanDefinition;
import java.lang.reflect.Constructor;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.NoOp;

/**
 * @description: Cglib实例化
 * @author: zzc
 * @modified By: zzc
 * @date: Created in 2021/11/20 16:29
 * @version:v1.0
 */
public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy{

    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor constructor, Object[] args) {

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(beanDefinition.getBeanClass());
        enhancer.setCallback(new NoOp() {
            @Override
            public int hashCode() {
                return super.hashCode();
            }

        });
        if(null == constructor) return enhancer.create();
        return enhancer.create(constructor.getParameterTypes(), args);
    }

}
